// pages/payForBuyCar/payForBuyCar.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPaying: false,
    isLoading: 1,//是否显示加载中图标
    cash_money:'',
    amount_money:'',//
    isPayed: 0,//是否已经支付过了
    goods_money: '',//商品总价格
    express_money: '',//运费总价格
    pay_money: '',//需要支付的总价格
    shopList: [],//存储店铺列表
    is_all: 0,//是否是全部商品
    goods_ids: '',//部分商品id
    cart_ids:'',//要结算的属性名片ID
    money: '',//需要支付的购物车金额
    filesSourceUri: '', //存储静态资源文件地址
    freight: '',//到付运费
    goodsDetail: {},//存储商品详情
    addressDetail: {},//存储地址详情
    goods_level: 0,//订单类型
    is_use_enve: 0,//是否使用红包余额 0未使用 1使用
    payTitle: '',//支付金额标题
    payDetail: '',//支付金额详情
    // payMoney: '',//需要支付的金额
    // content:'每人限领1件哦~',
    page: 1,
    recommendList: [],
    orderId: 0,//订单ID
    platform: '',
    order_money: ''
  },
  // 申请发货
  deliverGood: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    var goodsDetail = _this.data.goodsDetail;
    wx.showModal({
      title: '确定申请发货',
      content: '在收到东西时，需要您承担快递费用',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          goodsDetail.order_status = 2;
          goodsDetail.is_apply_send = 1,
            _this.setData({
              goodsDetail: goodsDetail
            })
          _this.applySendGoods();
        } else if (res.cancel) {
        }
      }
    })
  },
  // 申请发货调用接口
  applySendGoods: function (e) {
    var _this = this;
    var requestData = {
      token: _this.data.token, version: app.setConfig.version,
      type: app.setConfig.type,
      order_id: _this.data.orderId
    }
    app.post('/Order/applySendGoods', requestData, function (res) {
      if (res.code == 2000) {
        wx.showToast({
          title: '成功收货',
          icon: 'success',
          duration: 2000,
          success: function () {
            _this.getOrderDetail();
          }
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 返回首页
  goHome: function () {
    var _this = this;
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },
  // 免费领按钮
  freeReceive: function (e) {
    // console.log(e);
    var _this = this;
    var theGoodsId = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + theGoodsId,
    })
  },
  // 点击完成按钮
  completed: function () {
    console.log(1);
  },
  // 去往地址列表页设置收货地址
  goSetAddress: function () {
    var _this = this;
    var orderId = _this.data.orderId;
    var goodsDetail = _this.data.goodsDetail;
    wx.navigateTo({
      url: '/pages/addressList/addressList?orderId=' + orderId,
    })
  },
  // 确认收货
  confirmReceipt: function (e) {
    var _this = this;
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    var requestData = {
      token: _this.data.token,
      order_id: _this.data.orderId
    }
    app.post('/Order/takeGoods', requestData, function (res) {
      if (res.code == 2000) {
        var goodsDetail = _this.data.goodsDetail;
        goodsDetail.order_status = 3;
        _this.setData({
          goodsDetail: goodsDetail
        })
        wx.showToast({
          title: '成功收货',
          icon: 'success',
          duration: 2000,
          success: function () {
            _this.getOrderDetail();
          }
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 获取购物车订单详情
  getOrderDetail: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      is_all: _this.data.is_all,
      cart_ids: _this.data.cart_ids,
      goods_ids: _this.data.goods_ids
    }
    app.post('/ShopCart/cartOrderInfo', requestData, function (res) {
      if (res.code == 2000) {
        _this.setData({
          isLoading:0,
          amount_money: res.data.cart[0].amount_money,
          cash_money: res.data.cart[0].cash_money,
          shopList: res.data.cart,
          addressDetail: res.data.user_address,
          express_money: res.data.express_money,
          goods_money: res.data.goods_money,
          pay_money: res.data.pay_money,
          payTitle: '支付' + res.data.pay_money + '元',
          payDetail: '付款金额=商品' + res.data.goods_money + '元+运费' + res.data.express_money + '元'
        })
        _this.getRecommendList();
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    })
  },
  // 获取商品推荐列表
  getRecommendList: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      search_type:0,
      page: _this.data.page
    }
    app.post('/Goods/recommend', requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        var recommendList = _this.data.recommendList;

        recommendList = recommendList.concat(res.data.list);
        _this.setData({
          page: res.data.page,
          recommendList: recommendList
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 支付费用
  payForcost: function (e) {
    var _this = this;
    _this.setData({
      isPaying: true
    })
    var formId = e.detail.formId;
    app.postFormId(_this.data.token, formId);
    var requestData = {
      version: app.setConfig.version,
      token: _this.data.token,
      type: app.setConfig.type,
      money: _this.data.pay_money,
      is_all: _this.data.is_all,
      cart_ids: _this.data.cart_ids,
      goods_ids: _this.data.goods_ids
    }
    app.post('/ShopCart/goPay', requestData, function (res) {
      console.log(res);
      var goodsDetail = _this.data.goodsDetail;
      if (res.code == 2000) {
        var payInfo = res.data;
        _this.setData({
            isPaying: true
        })
        if (payInfo.respay) {
          wx.requestPayment({
            'timeStamp': payInfo.respay.timeStamp,
            'nonceStr': payInfo.respay.nonceStr,
            'package': payInfo.respay.package,
            'signType': 'MD5',
            'paySign': payInfo.respay.paySign,
            'success': function (res) {
              _this.setData({
                isPaying: true,
                isPayed: 1
              })
            },
            'fail': function (res) {
              _this.setData({
                isPaying: false,
                isPayed: 0
              })
            }
          });
        } else {
          _this.setData({
            isPaying: false,
            isPayed: 1
          })
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 1500
          })
        }

      } else {
        _this.setData({
          isPaying: false,
        })
        wx.showToast({
          title: res.msg,
          icon: 'none',
          mask: true,
          duration: 1500
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          platform: res.platform
        })
      },
    })
    var configs = wx.getStorageSync('configs');
    _this.setData({
      filesSourceUri: configs.static_url_http,
      cart_ids: options.cart_ids,
      goods_ids: options.goods_ids,
      is_all: options.is_all,
      money: options.money
    });
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    var theRegionUnit = _this.data.regionUnit;
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.getOrderDetail();
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var _this = this;
    _this.loop();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var _this = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    _this.setData({
      page: 1,
      recommendList: []
    })
    _this.getRecommendList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    // console.log('1');
    _this.getRecommendList();
  },
})